[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Types                    Define Data Types                    Program Section

    Standard Pascal allows you to define your own data types in your
    program or subprogram. There are six kinds of data types:

         *  renaming of existing data types
         *  subranges of existing data types
         *  enumerated data types
         *  named data structures (string, array, record, set)
         *  named pointer types
         *  named file types

    A data type is NOT a variable; therefore, no memory is allocated when
    you declare a data type, nor can you assign values to a data type
    name. Instead, having declared a data type, you can now declare
    variables (and parameters) to be of that type.

    Data types are declared in the declaration section of the program or
    subprogram. A list of type declarations is preceded by the word TYPE
    and uses the following syntax:

         type
           <tname>   = <tdecl>;
           <tname>   = <tdecl>;
           { etc. }

    where <tname> is any legal identifier and <tdecl> is a legal type
    declaration. Here's the specific format for each type declaration:

    * Renaming of existing data types:  <tdecl> is simply the name of an
      existing data type, such as

         type
           Meters = Real;
           Truth  = Boolean;
           ASCII  = Char;

    * Enumerated data types:  <tdecl> takes the format

         (<id1>,<id2>,...,<idN>)

    where <id1>, etc., are legal identifiers not previously declared. You
    are limited to 256 identifiers for a given enumerated type. Examples:

         type
           Days    = (Sun,Mon,Tues,Wed,Thur,Fri,Sat);
           Colors  = (Red,Orange,Yellow,Green,Blue,Indigo,Violet);
           Months  = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);

    * Subranges of existing data types:  <tdecl> takes the format

         <val1>..<val2>

    where <val1> and <val2> are values of any ordinal data type (Integer,
    Char, Boolean, or enumerated) and <val1> <= <val2>. Examples:

         type
           Byte     = 0..255;    { Turbo Pascal defines this for you }
           Year     = 1980..2099;
           Upper    = 'A'..'Z';
           Days     = (Sun,Mon,Tues,Wed,Thur,Fri,Sat);      { basetype
    for WeekDays }
           WeekDays = Mon..Fri;

    * Named data structures:  <tdecl> takes the same format as if you were
      declaring variables of this type:

         string[<maxsize>];
         array[<indices>] of <basetype>;
         record <field def>; <field def>; ...; <field def> end;
         set of <basetype>;

    If you wish to pass data structures as parameters, you must first
    declare them as a data type, then make sure both the formal and actual
    parameters are of that type. Examples:

         type
           NameStr   = string[20];
           SSStr     = string[9];

           Student   = record
             Last,First : NameStr;
             SSN        : SSStr;
             GPA        : Real;
             Passing    : Boolean
           end;

         Class     = array[1..100] of Student;
         CharSet   = set of Char;

    * Named pointer types:  <tdecl> takes the same format as if you were
      declaring variables of that type:

         ^<basetype>

    where <basetype> is any declared data type except 'text'. (You can
    actually have a pointer to type text, but you must first rename it.)
    Examples:

         type
           IntPtr     = ^Integer;
           CharPtr    = ^Char;
           NumList    = array[1..1000] of Integer;
           ListPtr    = ^NumList;

    In order to facilitate the creation of data types for linked lists,
    Pascal does not require that <basetype> be previously declared;
    instead, it merely requires that <basetype> be declared within that
    TYPE section. For example:

         type
           NodePtr    = ^Node;
           Node       = record
             Key       : Char;
             Data      : Integer;
             Next,Last : NodePtr
           end;

    * Named file types:  <tdecl> takes the same format as if you were
      declaring file variables:

         text;
         file of <basetype>;
         file;

    Having done this, you can now pass variables of this type as
    parameters.

    In Standard Pascal, you can have only one TYPE section in a program
    (or subprogram), and it must come after the CONST section and before
    the VAR section. In Turbo Pascal, you can have as many TYPE sections
    as you want, and there are no ordering restrictions.

See Also: type constants variables
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson